import java.util.concurrent.*;

class MyThreadPool{
    BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(4);

    public void submit(Runnable runnable){
        try {
            blockingQueue.put(runnable);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(()->{
                try {
                    Runnable a = blockingQueue.take();
                    a.run();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();
        }
    }
}
public class Demo {
    public static void main(String[] args) {
        ExecutorService service = Executors.newCachedThreadPool();//
        ExecutorService service1 = Executors.newFixedThreadPool(3);//线程池中的线程是固定的
        ExecutorService service2 = Executors.newSingleThreadExecutor();//线程池中有一个线程
        ExecutorService service3 = Executors.newScheduledThreadPool(2);//类似于定时器，只不过他有多个线程执行
        service.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("111");
            }
        });

        service1.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("222");
            }
        });

        service2.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("333");
            }
        });

        service3.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("444");
            }
        });
    }
}
